angular.module('starter.controllers', [])

    /***** 注册 *****/
    .controller('RegisterCtrl', function($scope, $location, $interval, $ionicLoading, $ChkCommon, $VerifyCode, $Register, Storage) {
        $scope.chkData = {
            'mobile_chk': false,
            'pwd_chk': false,
            'protocol_chk': true,
            'reg_ok': true
        };

        $scope.user = {
            'mobile': '',
            'password1': '',
            'password2': '',
            'code': ''
        };
        $scope.paracont = "获取验证码";
        $scope.paraclass = "maibei-positive";
        $scope.click_ok = true;
        var second = 60, timePromise = undefined;

        $scope.sendCode = function () {
            $scope.chkData.mobile_chk = $ChkCommon.chkMobile($scope.user.mobile);
            if($scope.chkData.mobile_chk){
                $VerifyCode.sendCode($scope.user.mobile);
            }
        };

        $scope.$on('VerifyCode.sendCode', function(){
            timePromise = $interval(function(){
                if(second<=0){
                    $interval.cancel(timePromise);
                    timePromise = undefined;
                    second = 60;
                    $scope.paracont = "重发验证码";
                    $scope.paraclass = "maibei-positive";
                    $scope.click_ok = true;
                }else{
                    $scope.click_ok = false;
                    $scope.paracont = second + "秒";
                    $scope.paraclass = "maibei-hui";
                    second--;
                }
            },1000,100);
        });

        $scope.Register = function () {
            $scope.chkData.mobile_chk = $ChkCommon.chkMobile($scope.user.mobile);
            $scope.chkData.pwd_chk = $ChkCommon.chkPwdConfirm($scope.user.password1, $scope.user.password2);
            if($scope.user.code.length != 6){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "亲，验证码为6位数字哦",
                    duration: 1500
                });
            }else if(!$scope.chkData.protocol_chk){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "亲，必须同意协议才可以注册哦",
                    duration: 1500
                });
            }else if($scope.chkData.mobile_chk && $scope.chkData.pwd_chk){
                $scope.chkData.reg_ok = false;
                $Register.signUp($scope.user.mobile, $scope.user.code, $scope.user.password1);
            }
        };

        $scope.$on('Register.signUp', function(){
            $scope.uid = $Register.getResult();
            if($scope.uid && $scope.uid.customer_id){
                Storage.set('uid', $scope.uid.customer_id);
                $location.path('/apply');
                $location.replace();
            }else{
                $scope.chkData.reg_ok = true;
            }
        })
    })

    /***** 登陆 *****/
    .controller('LoginCtrl', function($scope, $location, $ChkCommon, $Login, Storage) {
        $scope.eye_disabled = true;
        $scope.pass_type = 'password';
        $scope.login_disabled = true;
        $scope.user = {
            mobile: '',
            password: ''
        };

        var storageKey='uid';
        var uid = Storage.get(storageKey);
        if(uid){
            Storage.remove(storageKey);
        }

        //监控是否符合登陆点击要求
        var log_ok =$scope.$watch('user.mobile+user.password',function(){
            $scope.login_disabled = !($scope.user.mobile.length == 11 && $scope.user.password.length >= 6);
        });

        //密码可见与否控制
        $scope.changeEye = function () {
            $scope.eye_disabled = $scope.eye_disabled ? false : true;
            $scope.pass_type = $scope.pass_type == 'password'? 'text': 'password';
            console.log($scope.eye_disabled);
        };

        //登陆操作
        $scope.login = function () {
            var mobile_ok = $ChkCommon.chkMobile($scope.user.mobile);
            var pwd_ok = $ChkCommon.chkPassword($scope.user.password);
            if(mobile_ok && pwd_ok){
                $scope.login_disabled = true;
                $Login.signIn($scope.user.mobile, $scope.user.password);
            }
        };

        $scope.$on('Login.signIn', function(){
            $scope.uinfo = $Login.getResult();
            if($scope.uinfo && $scope.uinfo.customer.id){
                log_ok();
                Storage.set('uid', $scope.uinfo.customer.id);
                $location.path('/apply');
                $location.replace();
            }else{
                $scope.login_disabled= false;
            }
        })
    })

    /***** 密码找回第一页 *****/
    .controller('ResetPwdOneCtrl', function($scope, $ionicLoading, $location, Storage) {

    })

    /***** 密码找回第二页 *****/
    .controller('ResetPwdTwoCtrl', function($scope, $ionicLoading, $location, Storage) {

    })

    /***** 申请提现 *****/
    .controller('applyWithdrawalCtrl', function($scope, $state, $location, $applyInit, $authInit, Storage) {
        var amount;
        var storageKey='uid';
        var uid = Storage.get(storageKey);
        if(uid){
            //检测是否已下单及下单状态监测
            $authInit.getOrderStatus(uid);
            $scope.$on('apply.getOrderStatus', function () {
                $scope.u_status = $authInit.getStatusData();
                Storage.set('consume_id', $scope.u_status.consume_id);
                if($scope.u_status.consume_id == 0){
                    //加载初始化数据
                    $applyInit.getProducts(1, 0); //初始化获取产品类型
                }else{
                    $location.path('/orderRes');
                    $location.replace();
                }
            });
        }else{
            Storage.remove('consume_id');
            //加载初始化数据
            $applyInit.getProducts(1, 0); //初始化获取产品类型
        }

        $scope.$on('applyInit.getProducts', function () {
            $scope.res_data = $applyInit.getResult();
            $scope.data = { 'volume': $scope.res_data.def_cash/$scope.res_data.unit, 'selProduct': 0};
            amount = $scope.$watch('data.volume+data.selProduct',function(){
                $scope.cash_data = $scope.res_data.data[$scope.data.selProduct].cash_data;
                $scope.fee_amount = $scope.cash_data[$scope.data.volume-1].repay_total/100;
                Storage.remove('amount');
                Storage.set('amount', $scope.data.volume*$scope.res_data.unit);
            });
        });

        //下一步
        $scope.next = function () {
            uid = Storage.get(storageKey);
            //检查用户是否已经登陆
            if(!uid){
                $state.go('login');
                return false;
            }

            //当前认证进度查询
            $authInit.getAuthInfo(uid);
        };

        $scope.$on('applyInit.getAuthInfo', function () {
            $scope.auth_info = $authInit.getAuthData();

            //联系人信息
            if($scope.auth_info.code == 229 || $scope.auth_info.data.extra_info == 0){
                $state.go('extro_info');
            }
            //身份认证
            else if($scope.auth_info.data.id_num == 0){
                $state.go('credit_idnum');
            }
            //授权认证
            else{
                Storage.set('credit_must', $scope.auth_info.data.credit_must);
                $state.go('credit_auth');
            }
        });
    })

    /***** 身份认证 *****/
    .controller('creditIdNumCtrl', function($scope, $ionicLoading, $state, $location, $idNum, Storage) {
        $scope.user = {'name':'', 'id_num':''};
        //检查用户是否已经登陆
        var storageKey='uid';
        var uid = Storage.get(storageKey);
        if(!uid){
            $state.go('login');
            return false;
        }
        $scope.user.uid = uid;
        var next_ok;
        $scope.next_disabled = true;
            next_ok =$scope.$watch('user.name+user.id_num',function(){
            $scope.next_disabled = !($scope.user.name.length >= 2 && $scope.user.id_num.length == 18);
        });

        //下一步校验及信息保存
        $scope.next = function () {
            if($scope.user.name.length <= 0){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "亲，请输入您的姓名",
                    duration: 1500
                });
                return false;
            }
            if($scope.user.id_num.length != 18){
                $ionicLoading.show({
                    noBackdrop: true,
                    template: "亲，请输入正确的二代身份证号码",
                    duration: 1500
                });
                return false;
            }
            //保存用户信息
            $idNum.saveIdNum($scope.user);
        };
        
        $scope.$on('idNum.saveIdNum', function () {
            $location.path('/auth');
            $location.replace();
        })
    })

    /***** 联系人信息 *****/
    .controller('extInfoCtrl', function($scope, $state, $extroInfo, $location, Storage) {

        $scope.user = {'wechat':'', 'user_address':'', 'company_name':'', 'company_phone':'',
            'parent_name':'', 'parent_phone':'', 'parent_address':'', 'friends_name':'', 'friends_phone':''};

        //检查用户是否已经登陆
        var storageKey='uid';
        var uid = Storage.get(storageKey);
        if(!uid){
            $state.go('login');
            return false;
        }
        $scope.user.uid = uid;

        //下一步保存联系人信息
        $scope.next = function () {
            $extroInfo.saveExtInfo($scope.user);
        };

        $scope.$on('extroInfo.saveExtInfo', function () {
            $location.path('/cr_idnum');
            $location.replace();
        })
    })

    /***** 授权认证 *****/
    .controller('creditAuthCtrl', function($scope, $state, $location, $window, $timeout, $creditAuth, $authInit, Storage) {
        //检查用户是否已经登陆
        var storageKey='uid';
        var uid = Storage.get(storageKey);
        if(!uid){
            $state.go('login');
            return false;
        }
        $scope.uid = uid;
        $authInit.getAuthInfo($scope.uid);

        $scope.$on('applyInit.getAuthInfo', function () {
            $scope.user_auth = $authInit.getAuthData();
            if($scope.user_auth.code == 229){
                alert($scope.user_auth.msg);
                $timeout(function(){
                    $location.path('/ext_info');
                    $location.replace();
                },1500);

                return false;
            }
            $creditAuth.getAuthList($scope.uid);
        });

        $scope.my_auth = function (name, title_name) {
            if(name.lastIndexOf('http') != -1){
                $state.go('auth_tmp', {title: title_name, url:name});
            }else{
                $location.path(name);
            }
        };

        $scope.auth_list_all = [];
        $scope.local = angular.isUndefined($window.returnResult);
        $scope.$on('creditAuth.getAuthList', function () {
            var auth_list = $creditAuth.getAuthRes();
            if(!$scope.local){
                angular.forEach(auth_list, function (data, index) {
                    if(data.id == 'china_mobile' && data.status == 1){
                        data['src'] = './img/credit/web_phone.png';
                        data['u_auth'] = $scope.user_auth.data.china_mobile;
                    }else if(data.id == 'taobao_pass' && data.status == 1){
                        data['src'] = './img/credit/web_taobao.png';
                        data['u_auth'] = $scope.user_auth.data.taobao_pass;
                    }else if(data.id == 'jd_pass' && data.status == 1){
                        data['src'] = './img/credit/web_jingdong.png';
                        data['u_auth'] = $scope.user_auth.data.jd_pass;
                    }else if(data.id == 'alipay_pass' && data.status == 1){
                        data['src'] = './img/credit/web_alipay.png';
                        data['u_auth'] = $scope.user_auth.data.alipay_pass;
                    }else if(data.id == 'zhima_pass' && data.status == 1){
                        data['src'] = './img/credit/web_grade.png';
                        data['u_auth'] = $scope.user_auth.data.zhima_pass;
                    }
                    $scope.auth_list_all[index] = data;
                });
            }else{
                var u_auth_index = 0;
                angular.forEach(auth_list, function (data, index) {
                    if(data.id == 'china_mobile' && data.status == 1){
                        data['title'] = '运营商认证';
                        data['src'] = './img/credit/web_phone.png';
                        data['u_auth'] = $scope.user_auth.data.china_mobile;
                        if(data.url.indexOf('qtqapi') != -1){
                            u_auth_index = data.url.lastIndexOf("#/");
                            data['url'] = data.url.substring(u_auth_index+1);
                        }
                    }else if(data.id == 'taobao_pass' && data.status == 1){
                        data['title'] = '淘宝认证';
                        data['src'] = './img/credit/web_taobao.png';
                        data['u_auth'] = $scope.user_auth.data.taobao_pass;
                    }else if(data.id == 'jd_pass' && data.status == 1){
                        data['title'] = '京东认证';
                        data['src'] = './img/credit/web_jingdong.png';
                        data['u_auth'] = $scope.user_auth.data.jd_pass;
                    }else if(data.id == 'alipay_pass' && data.status == 1){
                        data['title'] = '阿里认证';
                        data['src'] = './img/credit/web_alipay.png';
                        data['u_auth'] = $scope.user_auth.data.alipay_pass;
                    }else if(data.id == 'zhima_pass' && data.status == 1){
                        data['title'] = '芝麻认证';
                        data['src'] = './img/credit/web_grade.png';
                        data['u_auth'] = $scope.user_auth.data.zhima_pass;
                        if(data.url.indexOf('qtqapi') != -1) {
                            u_auth_index = data.url.lastIndexOf("#");
                            data['url'] = data.url.substring(u_auth_index+1);
                        }
                    }
                    $scope.auth_list_all[index] = data;
                });
            }
        });
        
        $scope.next = function () {
            $location.path('/setppwd');
            $location.replace();
        };
    })

    /***** 设置支付密码 *****/
    .controller('setPPwdCtrl', function($scope, $state, $location, $ChkCommon, $setPayPass, Storage) {
        $scope.user = {
            password1: '',
            password2: ''
        };

        //检查用户是否已经登陆
        var storageKey='uid';
        var uid = Storage.get(storageKey);
        if(!uid){
            $state.go('login');
            return false;
        }
        $scope.user.uid = uid;

        //设置支付密码
        $scope.apply = function () {
            var pwd_ok = $ChkCommon.chkPwdConfirm($scope.user.password1, $scope.user.password2);
            if(pwd_ok){
                $setPayPass.setPayPass($scope.user.uid, $scope.user.password1);
            }
        };

        //跳转到下单页面
        $scope.$on('setPayPass.setPayPass', function () {
            var md5_pass = hex_md5($scope.user.password1);
            var sha1_pass = hex_sha1(md5_pass);
            var split_pass = sha1_pass.split("").reverse().join("");
            var pay_pass = split_pass.toUpperCase();
            Storage.set('pay_pwd', pay_pass);
            $location.path('/orderRes');
            $location.replace();
        })
    })

    /***** 下单并返回结果 *****/
    .controller('orderResCtrl', function($rootScope, $scope, $state, $location, $ionicLoading, $ionicPopup, $Dialog, $BaiduMap, $ChkCommon, $authInit, $apply, Storage) {
        $scope.u_status = {'status': 999};
        //检查用户是否已经登陆
        var storageKey='uid';
        var consume_id = Storage.get('consume_id');
        var uid = Storage.get(storageKey);
        if(!uid){
            $location.path('/login');
            $location.replace();
            return false;
        }

        $ionicLoading.show();

        $authInit.getOrderStatus(uid);
        $scope.$on('apply.getOrderStatus', function () {
            $scope.p_status = $authInit.getStatusData();
            if($scope.p_status.status == 6 || $scope.p_status.status == 7){
                $ionicLoading.hide();
                $scope.succ = false;
                $scope.pop_src = './img/order/audit_fail.png';
                $scope.showPopLayer('知道了', $scope.p_status.status, $scope.p_status.reason, 'er-popup');
            }else if($scope.p_status.status == 12 && $scope.p_status.consume_id > 0){
                $ionicLoading.hide();
                $scope.succ = true;
                $scope.pop_src = './img/order/audit_succeed.png';
                $scope.showPopLayer('下载APP提现', $scope.p_status.status, $scope.p_status.amount/100, 'su-popup');
            }else if($scope.p_status.status == 5){
                $ionicLoading.hide();
                $scope.u_status.status = 5;
            }else{
                $BaiduMap.getAddr();
            }
        });

        $scope.$on('BaiduMap.getCurrentPosition', function () {
            $scope.position = $BaiduMap.getAddrData();
            if($scope.position.length == 0){
                $ionicLoading.hide();
                $scope.u_status.status = 0;
                $scope.u_status.reason = '哎呀，定位失败,小钱钱飞走了，~(>_<)~';
            }else{
                $apply.applyOrder(uid, Storage.get('pay_pwd'), Storage.get('amount'), $scope.position);
            }
        });

        $scope.$on('BaiduMap.getLocationFailed', function () {
            $ionicLoading.hide();
            $scope.u_status.status = 0;
            $scope.u_status.reason = '哎呀，定位失败,小钱钱飞走了，~(>_<)~';
        });

        $scope.$on('apply.applyOrder', function () {
            $ionicLoading.hide();
            $scope.res = $apply.getRes();
            if($scope.res.code != 0){
                $scope.succ = false;
                $scope.pop_src = './img/order/audit_fail.png';
                $scope.showPopLayer('知道了', 0, $scope.res.msg, 'er-popup');
            }else if($scope.res.data.is_need_verify == 1){
                $scope.u_status.status = 5;
            }else if($scope.res.data.is_need_verify == 0){
                $scope.succ = true;
                $scope.u_status.status = 12;
                $scope.pop_src = './img/order/audit_succeed.png';
                $scope.showPopLayer('下载APP提现', $scope.u_status.status, $scope.res.data.amount/100, 'su-popup');
            }
        });

        $scope.showPopLayer = function(text, status, reason, class_name) {
            $ionicPopup.show({
                cssClass: class_name,
                template: '<img class="popup" src="{{pop_src}}"><div ng-if="succ" class="maibei-tips  maibei-center">' +
                '<div class="maibei-font-xx-large light maibei-hongbao">'+reason+'元</div>恭喜您，获得<span class="energized">'+reason+'</span>元的取现额度</div>' +
                '<div ng-if="!succ" class="maibei-tips  maibei-center">'+reason+'</div><div class="qtq-tips">趣提钱客服咨询热线<br/>13001137644<br/></div>',
                scope: $scope,
                /*buttons: [{
                    text: text,
                    onTap: function(e) {
                        if($scope.succ){
                            e.preventDefault();
                            location.href = 'http://dev.api.9maibei.com/Public/Download/index.html';
                        }
                        $scope.u_status.status = status;
                        $scope.u_status.reason = reason;
                    }
                }]*/
            });
        };

    })

    /***** 运营商认证 *****/
    .controller('chinaMobileCtrl', function($scope, $state, $ionicLoading, $location, $timeout, $window, $ChkCommon, $chmAuth, ENV){
        $scope.user = {'mobile': '', 'ch_pwd': '', 'ch_captcha': '', 'ch_q_pwd': '', 'ch_image': ''};
        $scope.auth_disabled = false;
        $scope.show = ENV.first;
        $scope.token = '';
        $scope.title_show = angular.isUndefined($window.returnResult);
        console.log($scope.title_show);
        $scope.type = $scope.title_show ? 2 : 1;
        $scope.source_type = 'LHP';
        $scope.lhp_type = 0;
        $scope.lhp_init = -1;
        $scope.data = {};
        $scope.chm_auth = function () {
            $scope.auth_disabled = true;
            var res = $ChkCommon.chkMobile($scope.user.mobile);
            if(res){
                if(!($scope.captcha || $scope.q_pwd) && $scope.user.ch_pwd.length < 4){
                    $scope.auth_disabled = false;
                    maibei_alert('亲，请输入正确的服务密码', 3000);
                    return false;
                }else if($scope.captcha && $scope.user.ch_captcha.length < 4){
                    $scope.auth_disabled = false;
                    maibei_alert('亲，请输入正确的短信验证码', 3000);
                    return false;
                }else if($scope.q_pwd && $scope.user.ch_q_pwd.length < 4){
                    $scope.auth_disabled = false;
                    maibei_alert('亲，请输入正确的查询密码', 3000);
                    return false;
                }

                if (!$scope.title_show) {
                    //调用安卓代码
                    $window.returnResult.showDialog("认证中，请稍等...")
                }else{
                    $ionicLoading.show();
                }
                if($scope.source_type == 'LHP'){
                    $chmAuth.chinaMobileLHP($scope.user.mobile, $scope.user.ch_pwd, $scope.user.ch_captcha, $scope.user.ch_image, $scope.lhp_type, $scope.lhp_init);
                }else if($scope.source_type == 'DHB'){
                    $chmAuth.chinaMobileDHB($scope.user.mobile, $scope.user.ch_pwd, $scope.user.ch_captcha, $scope.user.ch_q_pwd, $scope.token, $scope.show, $scope.type);
                }/*else if($scope.source_type == 'JXL'){
                    $chmAuth.chinaMobileJXL($scope.user.mobile, $scope.user.ch_pwd, $scope.user.ch_captcha, $scope.user.ch_q_pwd, $scope.token, $scope.show, $scope.website);
                }*/
            }else{
                $scope.auth_disabled = false;
            }
        };

        $scope.$on('chmAuth.chinaMobileLHP', function () {
            $scope.res = $chmAuth.getRes();
            if (!$scope.title_show) {
                if($scope.res.code == 0){
                    $window.returnResult.dismissDialog();
                    if($scope.res.type == 1){
                        $scope.lhp_init = $scope.res.in_need;
                        $scope.lhp_type = 1;
                        $scope.auth_disabled = false;
                        $scope.image_src = $scope.res.imgcode;
                        maibei_alert('亲，请输入图片验证码', 3000);
                    }else if($scope.res.type == 2){
                        $scope.lhp_init = $scope.res.in_need;
                        $scope.captcha = true;
                        $scope.lhp_type = 2;
                        $scope.auth_disabled = false;
                        maibei_alert('亲，请输入短信验证码', 3000);
                    }else{
                        //调用安卓代码
                        $timeout(function(){
                            $window.returnResult.resultFunction();
                        },1500);
                    }
                }else if($scope.res.code == ENV.lhp_err_1 || $scope.res.code == ENV.lhp_err_2 || $scope.res.code == ENV.lhp_err_4){
                    $window.returnResult.dismissDialog();
                    $scope.auth_disabled = false;
                    maibei_alert($scope.res.msg, 3000);
                }else if($scope.res.code == ENV.lhp_err_3){
                    $window.returnResult.dismissDialog();
                    if($scope.res.type == 1){
                        $scope.lhp_type = 1;
                        $scope.image_src = $scope.res.imgcode;
                        $scope.user.ch_image = '';
                    }else if($scope.res.type == 2){
                        $scope.captcha = true;
                        $scope.lhp_type = 2;
                        $scope.ch_captcha = '';
                    }
                    $scope.auth_disabled = false;
                    maibei_alert($scope.res.msg, 3000);
                }else{
                    $scope.source_type = 'DHB';
                    $chmAuth.chinaMobileDHB($scope.user.mobile, $scope.user.ch_pwd, $scope.user.ch_captcha, $scope.user.ch_q_pwd, $scope.token, $scope.show, $scope.type);
                }
            }else{
                if($scope.res.code == 0){
                    $ionicLoading.hide();
                    if($scope.res.type == 1){
                        $scope.captcha = true;
                        $scope.lhp_type = 1;
                        $scope.auth_disabled = false;
                        maibei_alert('亲，请输入图片验证码', 3000);
                    }else if($scope.res.type == 2){
                        $scope.captcha = true;
                        $scope.lhp_type = 2;
                        $scope.auth_disabled = false;
                        maibei_alert('亲，请输入短信验证码', 3000);
                    }else{
                        maibei_alert('授权成功');
                        //页面跳转
                        $timeout(function(){
                            $location.path('/auth');
                            $location.replace();
                        },1500);
                    }
                }else if($scope.res.code == ENV.lhp_err_1 || $scope.res.code == ENV.lhp_err_2 || $scope.res.code == ENV.lhp_err_3 || $scope.res.code == ENV.lhp_err_4){
                    $ionicLoading.hide();
                    $scope.auth_disabled = false;
                    maibei_alert($scope.res.msg, 3000);
                }else{
                    $scope.source_type = 'DHB';
                    $chmAuth.chinaMobileDHB($scope.user.mobile, $scope.user.ch_pwd, $scope.user.ch_captcha, $scope.user.ch_q_pwd, $scope.token, $scope.show, $scope.type);
                }
            }
        });

        $scope.$on('chmAuth.chinaMobileDHB', function () {
            $scope.res = $chmAuth.getRes();
            if (!$scope.title_show) {
                if($scope.res.code == 0){
                    //调用安卓代码
                    $window.returnResult.dismissDialog();
                    $timeout(function(){
                        $window.returnResult.resultFunction();
                    },1500);
                }else if($scope.res.code == ENV.captcha1){
                    $window.returnResult.dismissDialog();
                    $scope.show = ENV.captcha1;
                    $scope.captcha = true;
                    $scope.token = $scope.res.data.token;
                    $scope.auth_disabled = false;
                    maibei_alert($scope.res.msg, 3000);
                }else if($scope.res.code == ENV.captcha2){
                    $window.returnResult.dismissDialog();
                    $scope.show = ENV.captcha2;
                    $scope.captcha = true;
                    $scope.token = $scope.res.data.token;
                    $scope.auth_disabled = false;
                    maibei_alert($scope.res.msg, 3000);
                }else if($scope.res.code == ENV.req_pwd){
                    $window.returnResult.dismissDialog();
                    $scope.show = ENV.req_pwd;
                    $scope.q_pwd = true;
                    $scope.token = $scope.res.data.token;
                    $scope.auth_disabled = false;
                    maibei_alert($scope.res.msg, 3000);
                }else if($scope.res.code == ENV.mobile_err){
                    $window.returnResult.dismissDialog();
                    $scope.show = ENV.mobile_err;
                    $scope.auth_disabled = false;
                    maibei_alert($scope.res.msg, 3000);
                }else{
                    $scope.auth_disabled = false;
                    /*
                        $scope.source_type = 'JXL';
                        $chmAuth.chinaMobileJXL($scope.user.mobile, $scope.user.ch_pwd, $scope.user.ch_captcha, $scope.user.ch_q_pwd, $scope.token, $scope.show);
                    */
                }
            }else{
                if($scope.res.code == 0){
                    $ionicLoading.hide();
                    //页面跳转
                    $timeout(function(){
                        $location.path('/auth');
                        $location.replace();
                    },1500);
                }else if($scope.res.code == ENV.captcha1){
                    $ionicLoading.hide();
                    $scope.show = $scope.res.code;
                    $scope.captcha = true;
                    $scope.auth_disabled = false;
                    $scope.token = $scope.res.data.token;
                    maibei_alert($scope.res.msg, 3000);
                }else if($scope.res.code == ENV.captcha2){
                    $ionicLoading.hide();
                    $scope.show = $scope.res.code;
                    $scope.captcha = true;
                    $scope.auth_disabled = false;
                    $scope.token = $scope.res.data.token;
                    maibei_alert($scope.res.msg, 3000);
                }else if($scope.res.code == ENV.req_pwd){
                    $ionicLoading.hide();
                    $scope.show = $scope.res.code;
                    $scope.q_pwd = true;
                    $scope.auth_disabled = false;
                    $scope.token = $scope.res.data.token;
                    maibei_alert($scope.res.msg, 3000);
                }else if($scope.res.code == ENV.mobile_err){
                    $ionicLoading.hide();
                    $scope.show = ENV.mobile_err;
                    $scope.auth_disabled = false;
                    maibei_alert($scope.res.msg, 3000);
                }else{
                    $scope.auth_disabled = false;
                    /*
                        $scope.source_type = 'JXL';
                        $chmAuth.chinaMobileJXL($scope.user.mobile, $scope.user.ch_pwd, $scope.user.ch_captcha, $scope.user.ch_q_pwd, $scope.token, $scope.show, $scope.website);
                    */
                }
            }
        });

        $scope.$on('chmAuth.chinaMobileJXL', function () {
            $scope.res = $chmAuth.getRes();
            if (!$scope.title_show) {
                $window.returnResult.dismissDialog();
                if($scope.res.code == 0){
                    //调用安卓代码
                    $timeout(function(){
                        $window.returnResult.resultFunction();
                    },1500);
                }else if($scope.res.code == ENV.jxl_err_1 || $scope.res.code == ENV.jxl_err_2
                    || $scope.res.code == ENV.jxl_err_3 || $scope.res.code == ENV.jxl_err_4){
                    $scope.show = $scope.res.code;
                    $scope.captcha = true;
                    $scope.auth_disabled = false;
                    $scope.token = $scope.res.data.token;
                    $scope.website = $scope.res.data.website;
                    maibei_alert($scope.res.msg, 3000);
                }else if($scope.res.code == ENV.jxl_err_5){
                    $scope.show = $scope.res.code;
                    $scope.q_pwd = true;
                    $scope.auth_disabled = false;
                    $scope.token = $scope.res.data.token;
                    $scope.website = $scope.res.data.website;
                    maibei_alert($scope.res.msg, 3000);
                }else{
                    $scope.auth_disabled = false;
                }
            }else{
                $ionicLoading.hide();
                if($scope.res.code == 0){
                    //页面跳转
                    $timeout(function(){
                        $location.path('/auth');
                        $location.replace();
                    },1500);
                }else if($scope.res.code == ENV.jxl_err_1 || $scope.res.code == ENV.jxl_err_2
                    || $scope.res.code == ENV.jxl_err_3 || $scope.res.code == ENV.jxl_err_4){
                    $scope.show = $scope.res.code;
                    $scope.captcha = true;
                    $scope.auth_disabled = false;
                    $scope.token = $scope.res.data.token;
                    $scope.website = $scope.res.data.website;
                    maibei_alert($scope.res.msg, 3000);
                }else if($scope.res.code == ENV.jxl_err_5){
                    $scope.show = $scope.res.code;
                    $scope.q_pwd = true;
                    $scope.auth_disabled = false;
                    $scope.token = $scope.res.data.token;
                    $scope.website = $scope.res.data.website;
                    maibei_alert($scope.res.msg, 3000);
                }else{
                    $scope.auth_disabled = false;
                }
            }
        })
    })

    /***** 运营商服务密码重置 *****/
    .controller('resetChMPwd', function($scope, $state, $window, $timeout, $ionicLoading, $interval, $location, $ChkCommon, $chmVerifyCode) {
        $scope.user = {
            'mobile': '',
            'password1': '',
            'password2': '',
            'code': ''
        };
        $scope.res = {'data': {'website': ''}};
        $scope.paracont = "获取验证码";
        $scope.paraclass = "maibei-positive";
        $scope.click_ok = true;
        $scope.reset_ok = false;
        $scope.title_show = angular.isUndefined($window.returnResult);
        var second = 60, timePromise = undefined;

        $scope.type = 1;
        $scope.sendCode = function () {
            $scope.mobile_chk = $ChkCommon.chkMobile($scope.user.mobile);
            if($scope.mobile_chk){
                $ionicLoading.show();
                $chmVerifyCode.sendCode($scope.user.mobile, $scope.type);
            }
        };

        $scope.$on('chmVerifyCode.sendCodeDHB', function(){
            $ionicLoading.hide();
            $scope.res = $chmVerifyCode.getCode();
            timePromise = $interval(function(){
                if(second<=0){
                    $interval.cancel(timePromise);
                    timePromise = undefined;
                    second = 60;
                    $scope.paracont = "重发验证码";
                    $scope.paraclass = "maibei-positive";
                    $scope.click_ok = true;
                    $scope.type = 2;
                }else{
                    $scope.click_ok = false;
                    $scope.paracont = second + "秒";
                    $scope.paraclass = "maibei-hui";
                    second--;
                }
            },1000,100);
        });

        $scope.$on('chmVerifyCode.sendCodeJXL', function(){
            $ionicLoading.hide();
            $scope.res = $chmVerifyCode.getCode();
            if($scope.res.code == 0){
                $scope.type = 3;
                timePromise = $interval(function(){
                    if(second<=0){
                        $interval.cancel(timePromise);
                        timePromise = undefined;
                        second = 60;
                        $scope.paracont = "重发验证码";
                        $scope.paraclass = "maibei-positive";
                        $scope.click_ok = true;
                    }else{
                        $scope.click_ok = false;
                        $scope.paracont = second + "秒";
                        $scope.paraclass = "maibei-hui";
                        second--;
                    }
                },1000,100);
            }else{
                $interval.cancel(timePromise);
                timePromise = undefined;
                second = 60;
                $scope.paracont = "重发验证码";
                $scope.paraclass = "maibei-positive";
                $scope.click_ok = true;
            }
        });

        $scope.resetChMPwd = function () {
            if($scope.type != 3){
                $scope.res.data.website = '';
            }
            $scope.mobile_chk = $ChkCommon.chkMobile($scope.user.mobile);
            if($scope.mobile_chk){
                if($scope.user.code.length != 6){
                    maibei_alert('亲，验证码为6位数字哦', 3000);
                    $scope.reset_ok = false;
                    return false;
                }else if($scope.user.password1.length != 6){
                    maibei_alert('亲，服务密码为6位数字哦', 3000);
                    $scope.reset_ok = false;
                    return false;
                }else if($scope.user.password1 != $scope.user.password2){
                    maibei_alert('亲，两次密码不匹配呢', 3000);
                    $scope.reset_ok = false;
                    return false;
                }
                $ionicLoading.show();
                $chmVerifyCode.resetPWD($scope.user.password1, $scope.res.data.token, $scope.user.code, $scope.res.data.website);
            }
        };

        $scope.$on('chmVerifyCode.resetPWD', function(){
            $ionicLoading.hide();
            $scope.res_data = $chmVerifyCode.getResDate();
            if($scope.res_data.code == 0){
                $timeout(function(){
                    $location.path('/chm_auth');
                    $location.replace();
                },1500);
            }else{
                $scope.reset_ok = false;
            }
        });
    })


    /***** 芝麻认证 *****/
    .controller('taobaoSesameCtrl', function($scope, $state, $ionicLoading, $window, $sesame, $location, ENV){
        $scope.user = {'name': '', 'id_num': ''};
        $scope.auth_disabled = false;
        $scope.show = ENV.first;
        $scope.token = '';
        $scope.title_show = angular.isUndefined($window.returnResult);
        console.log($scope.title_show);
        $scope.data = {};
        $scope.sesame_auth = function () {
            $scope.auth_disabled = true;
            if($scope.user.name.length <= 0 || $scope.user.id_num.length <= 0){
                $scope.auth_disabled = false;
                maibei_alert('亲，姓名和身份证号为必填项', 3000);
                return false;
            }

            if (!$scope.title_show) {
                //调用安卓代码
                $window.returnResult.showDialog("认证中，请稍等...")
            }else{
                $ionicLoading.show();
            }
            $sesame.sesameAuth($scope.user.id_num, $scope.user.name);
        };

        $scope.$on('sesema_auth', function () {
            $scope.res = $sesame.getResult();
            if (!$scope.title_show) {
                if($scope.res.code == 0) {
                    //调用安卓代码
                    $window.returnResult.dismissDialog();
                    location.href = $scope.res.data.url;
                    /*$timeout(function(){
                        $window.returnResult.resultFunction();
                    },1500);*/
                } else {
                    $window.returnResult.dismissDialog();
                    $scope.auth_disabled = false;
                    maibei_alert($scope.res.msg, 3000);
                }
            }else {
                if ($scope.res.code == 0) {
                    $ionicLoading.hide();
                    //页面跳转
                    location.href = $scope.res.data.url;
                    /*$timeout(function(){
                     $location.path('/auth');
                     $location.replace();
                     },1500);*/
                } else {
                    $ionicLoading.hide();
                    $scope.auth_disabled = false;
                    maibei_alert($scope.res.msg, 3000);
                }
            }
        });

        $scope.success = function () {
            if (!$scope.title_show) {
                //调用安卓代码
                $window.returnResult.resultFunction();
            }else {
                $location.path('/auth');
                $location.replace();
            }
        }
    })

    .controller('authTmpCtrl', function($scope, $stateParams, $sce){
        $scope.auth_url = $sce.trustAsResourceUrl($stateParams.url);
        $scope.title = $stateParams.title;
    })



;